package cn.webrx;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class T3 {
    public static void main(String[] args) throws Exception {
        ExecutorService es = Executors.newFixedThreadPool(10);
        es.execute(()->{
            System.out.println(Thread.currentThread().getName());
            try{
                TimeUnit.SECONDS.sleep(5);
                System.out.println("end");
            }catch(Exception e){

            }
        });//执行子线程任务
        es.execute(()->{
            System.out.println(Thread.currentThread().getName());
            try{
                TimeUnit.SECONDS.sleep(3);
                System.out.println("end");
            }catch(Exception e){

            }
        });//执行子线程任务
        es.execute(()->{
            System.out.println(Thread.currentThread().getName());
            try{
                TimeUnit.SECONDS.sleep(15);
                System.out.println("end");
            }catch(Exception e){

            }
        });//执行子线程任务

        try {
            es.shutdown();
            if(!es.awaitTermination(5, TimeUnit.SECONDS)){//5s
                System.out.println(" 到达指定时间，还有线程没执行完，不再等待，关闭线程池!");
                es.shutdownNow();
            }
        } catch (Throwable e) {     // TODO Auto-generated catch block

        }finally{
            System.out.println("线程池结束");
        }
    }
}
